package com.google.code.sip.ucenter.service.impl;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.google.code.sip.core.service.ServiceException;
import com.google.code.sip.ucenter.dao.SettingDao;
import com.google.code.sip.ucenter.dao.mongo.FriendDaoImpl;
import com.google.code.sip.ucenter.dao.mongo.FriendGroupDaoImpl;
import com.google.code.sip.ucenter.model.Friend;
import com.google.code.sip.ucenter.model.FriendGroup;
import com.google.code.sip.ucenter.model.Setting;
import com.google.code.sip.ucenter.model.User;
import com.google.code.sip.ucenter.service.FriendGroupManager;

@Service("friendGroupManager")
public class FriendGroupManagerImpl implements FriendGroupManager {
	private static Logger logger = LoggerFactory
			.getLogger(FriendGroupManagerImpl.class);

	@Autowired
	private FriendGroupDaoImpl friendGroupDao;

	@Autowired
	private FriendDaoImpl friendDao;

	@Autowired
	private SettingDao settingDao;

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.google.code.sip.ucenter.service.FriendGroupManager#addFriendGroup
	 * (java.lang.Long, java.lang.String, java.lang.String)
	 */
	@Override
	public void addFriendGroup(User user, String groupName, String des) {
		if (user != null) {
			FriendGroup friendGroup = new FriendGroup();
			friendGroup.setName(groupName);
			friendGroup.setUser(user);
			friendGroup.setDescription(des);
			friendGroup.setDefaulted(false);
			friendGroupDao.save(friendGroup);
		}

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.google.code.sip.ucenter.service.FriendGroupManager#getFriendGroups
	 * (java.lang.Long)
	 */
	@Override
	public List<FriendGroup> getFriendGroups(User user) {
		if (user != null) {
			return friendGroupDao.getFriendGroups(user);
		} else {
			return null;
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.google.code.sip.ucenter.service.FriendGroupManager#updateGroupName
	 * (java.lang.Long, java.lang.String)
	 */
	@Override
	public void updateGroupName(FriendGroup friendGroup, String groupName) {
		if (friendGroup != null) {
			friendGroup.setName(groupName);
			friendGroupDao.save(friendGroup);
		}

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.google.code.sip.ucenter.service.FriendGroupManager#get(java.lang.
	 * String)
	 */
	@Override
	public FriendGroup getFriendGroup(String id) {
		return friendGroupDao.get(id);
	}

	@Override
	public void deleteFriendGroup(String id) {
		FriendGroup friendGroup = getFriendGroup(id);
		if (friendGroup != null) {
			deleteFriendGroup(friendGroup);
		}
	}

	@Override
	public void deleteFriendGroup(FriendGroup friendGroup) {
		if (friendGroup.isDefaulted()) {
			logger.warn("不能删除缺省好友组" + friendGroup.getName() + "。");
			throw new ServiceException("不能删除缺省好友组" + friendGroup.getName()
					+ "。");
		}
		// 移动好友到缺省好友组
		List<Friend> friends = friendDao.getFriends(friendGroup);
		if (friends != null && !friends.isEmpty()) {
			FriendGroup defaultFriendGroup = getDefaultFriendGroup(friendGroup
					.getUser());
			if (defaultFriendGroup == null) {
				defaultFriendGroup = createDefaultGroup(friendGroup.getUser());
			}

			// 将好友组的好友转到默认组中去
			if (defaultFriendGroup != null) {
				for (Friend friend : friends) {
					friend.setFriendGroup(defaultFriendGroup);
					friendDao.save(friend);
				}
			}
		}
		friendGroupDao.delete(friendGroup);
	}

	@Override
	public FriendGroup getDefaultFriendGroup(User user) {
		Map<String, Object> filters = new LinkedHashMap<String, Object>();
		filters.put("user", user);
		filters.put("defaulted", true);
		return friendGroupDao.getByProperties(filters);
	}

	@Override
	public void createdCommonGroups(User user) {
		FriendGroup defaultFriendGroup = getDefaultFriendGroup(user);
		if (defaultFriendGroup == null) {
			createDefaultGroup(user);
		}
		Setting setting = settingDao
				.getByName(Setting.AUTO_CREATE_FRIEND_GROUP);
		String commonGroupNameString = setting.getValue();
		if (StringUtils.isNotBlank(commonGroupNameString)) {
			String[] groupNames = commonGroupNameString.split(",");
			for (String groupName : groupNames) {
				if (StringUtils.isNotBlank(groupName)) {
					FriendGroup group = getByUserAndGroupName(user, groupName);
					if (group == null) {
						group = new FriendGroup();
						group.setName(groupName);
						group.setUser(user);
						group.setDefaulted(false);
						group.setDescription(groupName);
						friendGroupDao.save(group);
					}
				}
			}
		}
	}

	public FriendGroup getByUserAndGroupName(User user, String groupName) {
		Map<String, Object> filters = new LinkedHashMap<String, Object>();
		filters.put("user", user);
		filters.put("name", groupName);
		return friendGroupDao.getByProperties(filters);
	}

	private FriendGroup createDefaultGroup(User user) {
		FriendGroup defaultFriendGroup = new FriendGroup();
		defaultFriendGroup.setName(FriendGroup.DEFAULT_FRIEND_GROUP_NAME);
		defaultFriendGroup.setUser(user);
		defaultFriendGroup
				.setDescription(FriendGroup.DEFAULT_FRIEND_GROUP_NAME);
		defaultFriendGroup.setDefaulted(true);
		friendGroupDao.save(defaultFriendGroup);
		return defaultFriendGroup;
	}
}
